//
//  CJInterstitialGMAdapter.m
//  CJModileGMAdapter
//
//  Created by shanghailingye on 2024/4/24.
//

#import "CJInterstitialToBidAdapter.h"

@interface CJInterstitialToBidAdapter()<CJInterstitialAdDelegate>

@property (nonatomic, weak) id<AWMCustomInterstitialAdapterBridge> bridge;
@property (nonatomic, strong) CJInterstitialAd *interstitialAd;
@property (nonatomic, assign) BOOL isReady;
// 是否是bidding
@property (nonatomic, assign) BOOL isBidding;

@end
@implementation CJInterstitialToBidAdapter
- (instancetype)initWithBridge:(id<AWMCustomInterstitialAdapterBridge>)bridge {
    self = [super init];
    if (self) {
        _bridge = bridge;
    }
    return self;
}

- (BOOL)mediatedAdStatus {
    return self.isReady;
}
- (void)loadAdWithPlacementId:(NSString *)placementId parameter:(AWMParameter *)parameter {
    self.isReady = false;
    self.isBidding = parameter.isHeaderBidding;
    self.interstitialAd = [[CJInterstitialAd alloc] initWithResourceId:placementId];
    self.interstitialAd.delegate = self;
    [self.interstitialAd loadAdData];
}
- (void)destory {
    self.interstitialAd = nil;
}

- (BOOL)showAdFromRootViewController:(UIViewController *)viewController parameter:(AWMParameter *)parameter {
    [self.interstitialAd showFromRootViewController:viewController];
    return true;
}

- (void)didReceiveBidResult:(AWMMediaBidResult *)result {
    NSLog(@"%@", result.win ? @"胜出" : @"竞胜失败");
}

#pragma mark CJInterstitialAdDelegate
- (void)interstitialAdDidLoad:(nonnull CJInterstitialAd *)interstitialAd resourceId:(nonnull NSString *)resourceId {
    NSInteger cpm = [self.interstitialAd getECpm];
    NSDictionary *ext = @{
        AWMMediaAdLoadingExtECPM : cpm ? [NSString stringWithFormat:@"%ld", cpm] : @"0",
    };
    if (self.isBidding) { [self.bridge interstitialAd:self didAdServerResponseWithExt:ext]; }
    self.isReady = true;
    [self.bridge interstitialAdDidLoad:self];
}

- (void)interstitialAdLoadFailed:(nonnull CJInterstitialAd *)interstitialAd error:(nonnull NSError *)error {
    [self.bridge interstitialAd:self didLoadFailWithError:error ext:@{}];
}

- (void)interstitialAdOnClicked:(nonnull CJInterstitialAd *)interstitialAd {
    [self.bridge interstitialAdDidClick:self];
}

- (void)interstitialAdOnClosed:(nonnull CJInterstitialAd *)interstitialAd {
    [self.bridge interstitialAdDidClose:self];
}

- (void)interstitialAdOnShow:(nonnull CJInterstitialAd *)interstitialAd {
    [self.bridge interstitialAdDidVisible:self];
}



@end
